class ProductosController < ApplicationController
  def index
    catalogo
    render :action => 'catalogo'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def catalogo
    if ["lavadora","frigo","tv","peque","dvd"].include? params[:tipo]
      @tipo = params[:tipo]
      @producto_pages, @productos = paginate :productos, :per_page => 10,
                                             :class_name => params[:tipo].camelize
    else
      @producto_pages, @productos = paginate :productos, :per_page => 10
    end
    @columnas = Producto.columns.delete_if \
       { |c| not Producto::CAMPOS_COMUNES.include? c.name and c.name != "type" }
  end

  def info
    if params[:id] == nil
      redirect_to :action => index
    else
      @producto = Producto.find(params[:id])
      tipo = @producto.class
      @columnas = Producto.columns.delete_if \
            { |c| not Producto::CAMPOS_COMUNES.include? c.name \
              and not tipo::CAMPOS_PROPIOS.include? c.name \
              and c.name != 'type' }
    end
  end
  
  def new
    @producto = Producto.new
  end

  def create
    @producto = Producto.new(params[:producto])
    if @producto.save
      flash[:notice] = 'Producto was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @producto = Producto.find(params[:id])
  end

  def update
    @producto = Producto.find(params[:id])
    if @producto.update_attributes(params[:producto])
      flash[:notice] = 'Producto was successfully updated.'
      redirect_to :action => 'show', :id => @producto
    else
      render :action => 'edit'
    end
  end

  def destroy
    Producto.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

end
